//
//  PhotoWallCollectionViewCell.m
//  Travel_M_Travel
//
//  Created by APPLE10 on 15-3-31.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "PhotoWallCollectionViewCell.h"

@interface PhotoWallCollectionViewCell ()<UIScrollViewDelegate>
@property (nonatomic , assign)BOOL flag;
@property (nonatomic , retain)UIView *headView;
@property (nonatomic , retain)UIView *footView;
@property (nonatomic, retain)UITextView *footTextView;
@property (nonatomic , retain)UILabel *footTimeLable;
@end

@implementation PhotoWallCollectionViewCell
-(void)dealloc
{
    Block_release(_checkblock);
    Block_release(_block);
    [_photoImageView release];
    [_photoData release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.flag = YES;
        self.backScrollView = [[UIScrollView alloc]init];
        self.backScrollView.backgroundColor = [UIColor blackColor];
        self.backScrollView.maximumZoomScale = 2.0;
        self.backScrollView.minimumZoomScale = 1.0;
        //协议一定记得签!!!!!!
        self.backScrollView.delegate =self;
        self.backScrollView.userInteractionEnabled = NO;
        self.backScrollView.showsHorizontalScrollIndicator = NO;
        self.backScrollView.showsVerticalScrollIndicator = NO;
        [self.contentView addSubview:_backScrollView];
        [_backScrollView release];
        
        
        self.photoImageView = [[UIImageView alloc]init];
        //        _photoImageView.backgroundColor = [UIColor yellowColor];
        _photoImageView.contentMode = UIViewContentModeScaleAspectFit;
        _photoImageView.image = [UIImage imageNamed:@"jiazai1.png"];
        _photoImageView.userInteractionEnabled = YES;
        [self.contentView addSubview:_photoImageView];
        [self.backScrollView addSubview:_photoImageView];
        [_photoImageView release];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [_photoImageView addGestureRecognizer:tap];
        //执行方法需要点击几次
        tap.numberOfTapsRequired = 1;
        //            //需要几个手指点击
        //            tap.numberOfTouchesRequired = 2;
        [tap release];
        
        self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 64)];
        //        _headView.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:_headView];
        
        CGRect backFram =CGRectMake(0, 20, 50, 30);
        UIButton *backBtu = [UIButton buttonWithTitle:@"返回" frame:backFram target:self action:@selector(backBtuAction:)];
        [backBtu setTintColor:[UIColor whiteColor]];
        //        backBtu.backgroundColor = [UIColor whiteColor];
        [_headView addSubview:backBtu];
        
        CGRect checkFram = CGRectMake(_headView.frame.size.width - 100, 20, 100, 30);
        UIButton *checkBtu = [UIButton buttonWithTitle:@"查看游记" frame:checkFram target:self action:@selector(checkBtuAction:)];
        [checkBtu setTintColor:[UIColor whiteColor]];
        [_headView addSubview:checkBtu];
        
        self.footView = [[UIView alloc]initWithFrame:CGRectMake(0, self.contentView.frame.size.height - 100, self.contentView.frame.size.width, 130)];
        //        _footView.backgroundColor = [UIColor yellowColor];
        self.footView.alpha = 0.5;
        [self.contentView addSubview:_footView];
        
        self.footTextView =[[UITextView alloc]initWithFrame:CGRectMake(0, 0, self.footView.frame.size.width, 90)];
        self.footTextView.editable = NO;
        self.footTextView.textColor = [UIColor whiteColor];
        self.footTextView.backgroundColor = [UIColor blackColor];
        [self.footView addSubview:_footTextView];
        
        self.footTimeLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 80, self.contentView.frame.size.width, 20)];
        self.footTimeLable.font = [UIFont fontWithName:@"Georgia" size:14];
        self.footTimeLable.textColor = [UIColor whiteColor];
        //        self.footTimeLable.backgroundColor = [UIColor yellowColor];
        [self.footView addSubview:_footTimeLable];
        
    }
    
    return self;
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    self.backScrollView.frame = layoutAttributes.bounds;
    //    self.photoImageView.frame = CGRectMake(0, 50, self.contentView.frame.size.width, self.contentView.frame.size.height - 100);
    self.photoImageView.frame = CGRectMake(5, 5, layoutAttributes.bounds.size.width - 10, layoutAttributes.bounds.size.height - 10);
}

- (void)setPhotoData:(JourneyPhotoDataSource *)photoData
{
    self.backScrollView.zoomScale = 1.0;
    
    if (_photoData != photoData) {
        [_photoData release];
        _photoData = [photoData retain];
    }
    
    NSString *str = _photoData.photo;
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
    [_photoImageView setImageWithURL:url placeholderImage:[UIImage imageNamed: @"jiazai1.png" ]];
    
    self.footTextView.text = _photoData.text;
    self.footTimeLable.text = _photoData.local_time;
}

- (void)setWayPointData_way:(WayPointDataSource_WayPoints *)wayPointData_way
{
    
    if (_wayPointData_way != wayPointData_way) {
        [_wayPointData_way release];
        _wayPointData_way = [wayPointData_way retain];
    }
    NSString *str = _wayPointData_way.photo;
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
    
    [_photoImageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"jiazai1.png"]];
}

- (void)setMode:(PhotosCVCellMode)mode
{
    _mode = mode;
    self.backScrollView.zoomScale = 1.0;
    if (mode == PhotosCVCellModeMax) {
        self.backScrollView.userInteractionEnabled = YES;
        self.photoImageView.contentMode = UIViewContentModeScaleAspectFit;
        self.contentView.backgroundColor = [UIColor blackColor];
    } else {
        self.backScrollView.userInteractionEnabled = NO;
        self.photoImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
}
- (void)backBtuAction:(UIButton *)btu
{
    self.block();
}
- (void)checkBtuAction : (UIButton *)btu
{
    self.checkblock();
}

-(void)tapAction : (UITapGestureRecognizer *)tap
{
    if (self.flag == YES) {
        self.headView.hidden = YES;
        self.footView.hidden = YES;
    }else
    {
        self.headView.hidden = NO;
        self.footView.hidden = NO;
    }
    self.flag = !self.flag;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    
    return [self.backScrollView.subviews firstObject];
}

@end
